/* ---------------------------------------顶部渲染--------------------------------------- */

window.onload = async function axio() {
    const res = await axios({
        url: '/dashboard',
        method: 'get',
    })

    console.log(res);//不信就LOG看一下
    const { year, salaryData, groupData, provinceData } = res.data //解构都快忘了,用声明同名的变量,对应对象里面同名的元素就可以完成解构

    // console.log(res.data.overview);//因为响应结果剥离过了一层,所以这里的得到的是剥离过的data里面的数据渲染应该也少些一层
    for (k in res.data.overview) {
        document.querySelector(`[name=${k}]`).innerHTML = res.data.overview[k]
    }
    /* ---------------------------------------顶部渲染--------------------------------------- */




    /* ---------------------------------------  薪资走势图函数  --------------------------------------- */
    function salaryEcharts(year) {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.querySelector('#line'));
        // 指定图表的配置项和数据
        const option = {
            //标题
            title: {
                text: '2021全学科薪资走势图',
                textStyle: {
                    fontSize: '20px',
                    color: 'black'
                },
                left: '3%',
                top: '4%'
            },
            yAxis: {
                type: 'value',
                //分隔线
                splitLine: {
                    //分割线样式
                    lineStyle: {
                        type: 'dashed', //类型
                        width: 1, //宽度
                        color: '#d3d3d3' //颜色
                    }
                }
            },
            tooltip: {
                show: true
            },
            xAxis: {
                type: 'category',
                data: year.map(v => v.month),
                //x轴线
                axisLine: {
                    //x轴线的样式
                    lineStyle: {
                        type: 'dashed',
                        color: '#d3d3d3'
                    }
                },
                //x轴底下标牌
                axisLabel: {
                    color: 'black',
                }
            },
            //图标
            grid: {
                //设置上下左右
                left: '10%',
                top: '20%'
            },
            series: [
                {
                    data: year.map(v => v.salary),
                    type: 'line',
                    smooth: true,
                    //圆圈大小
                    symbolSize: 10,
                    //线的大小
                    lineStyle: {
                        width: 4
                    },
                    //区域颜色
                    areaStyle: {
                        //渐变色
                        color: {
                            type: 'linear',
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            colorStops: [
                                {
                                    offset: 0,
                                    color: '#84c396' // 0% 处的颜色
                                },
                                {
                                    //区域大小
                                    offset: .8,
                                    color: '#fff' // 100% 处的颜色
                                }
                            ],
                            global: false // 缺省为 false
                        }
                    }
                }
            ]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
    salaryEcharts(year)
    /* ---------------------------------------  薪资走势图函数 --------------------------------------- */






    /* ---------------------------------------  班级薪资公布图函数  --------------------------------------- */
    function salaryPublish(salaryData) {
        const myChart = echarts.init(document.querySelector('#salary'));
        option = {
            tooltip: {
                trigger: 'item'
            },
            title: {
                top: '2%',
                left: '3%',
                text: '班级薪资公布'
            },
            legend: {
                bottom: '5%',
                left: 'center'
            },
            series: [
                {
                    name: 'Access From',
                    type: 'pie',
                    radius: ['60%', '70%'],
                    avoidLabelOverlap: false,
                    itemStyle: {
                        borderRadius: 10,
                        borderColor: '#fff',
                        borderWidth: 6
                    },
                    label: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        label: {
                            show: true,
                            fontSize: '40',
                            fontWeight: 'bold'
                        }
                    },
                    labelLine: {
                        show: false
                    },
                    data: salaryData.map(v => {
                        return { //要求数组里面是对象,这里也返回对象,并且按照要求的键值对格式返回
                            value: v.g_count + v.b_count,
                            name: v.label,
                        }
                    }),
                    color: ['#fda224', '#5097ff', '#3fceff', '#34d39a']
                }
            ]
        };

        myChart.setOption(option);
    }
    salaryPublish(salaryData)
    /* ---------------------------------------  班级薪资公布图函数  --------------------------------------- */





    /* ---------------------------------------  班级每组薪资函数  --------------------------------------- */
    function groupEchart(groupData) {
        const myChart = echarts.init(document.querySelector('#lines'));
        const option = {
            xAxis: {
                type: 'category',
                data: groupData[1].map(v => v.name),
                axisLine: {
                    lineStyle: {
                        color: '#d3d3d3',
                        type: 'dashed'
                    }
                }
            },

            yAxis: {
                type: 'value'
            },
            color: [
                {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                        {
                            offset: 0,
                            color: '#34d29a' // 0% 处的颜色
                        },
                        {
                            offset: 1,
                            color: '#d4f5ea' // 100% 处的颜色
                        }
                    ],
                    global: false // 缺省为 false
                },
                {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                        {
                            offset: 0,
                            color: '#499fed' // 0% 处的颜色
                        },
                        {
                            offset: 1,
                            color: '#d8eafb' // 100% 处的颜色
                        }
                    ],
                    global: false // 缺省为 false
                }
            ],
            series: [
                {
                    data: groupData[1].map(v => v.hope_salary),
                    type: 'bar'
                },
                {
                    type: 'bar',
                    data: groupData[1].map(v => v.salary)
                },
            ]
        };
        myChart.setOption(option);

        /* --------------------------------------- 点击高亮并且重新渲染图标   --------------------------------------- */
        const btn = document.querySelectorAll('#btns button')
        btn.forEach(v => {
            v.onclick = function () {
                console.log(this);
                btn.forEach(v => v.classList.remove('btn-blue'))
                this.classList.add('btn-blue')
                const index = this.innerHTML
                option.xAxis.data = groupData[index].map(v => v.name)
                option.series[0].data = groupData[index].map(v => v.hope_salary)
                option.series[1].data = groupData[index].map(v => v.salary)
                myChart.setOption(option);
            }
        })
        /* --------------------------------------- 点击高亮并且重新渲染图标   --------------------------------------- */
    }
    groupEchart(groupData)
    /* ---------------------------------------  班级每组薪资函数  --------------------------------------- */


    /* ---------------------------------------  男女分布  --------------------------------------- */
    function boyGirlEchart(salaryData) {
        const myChart = echarts.init(document.querySelector('#gender'));
        const option = {
            title: [{
                text: '男女薪资分布',
                left: '10%',
                top: '3%'
            },
            {
                text: '男生',
                left: '43%',
                top: '50%'
            },
            {
                text: '女生',
                left: '43%',
                bottom: '1%'
            }
            ],
            tooltip: {
                trigger: 'item'
            },
            legend: {
                show: false,
                orient: 'vertical',
                left: 'left'
            },
            color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a', 'yellowgreen'],
            series: [
                {
                    name: 'Access From',
                    type: 'pie',
                    radius: ['25%', '30%'],
                    center: ['50%', '80%'],
                    data: salaryData.map(v => {
                        return {
                            value: v.b_count,
                            name: v.label
                        }
                    }),
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                },
                {
                    name: 'Access From',
                    type: 'pie',
                    radius: ['25%', '30%'],
                    center: ['50%', '30%'],
                    data: salaryData.map(v => {
                        return {
                            value: v.g_count,
                            name: v.label
                        }
                    }),
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };


        myChart.setOption(option);
    }
    boyGirlEchart(salaryData)
    /* ---------------------------------------  男女分布  --------------------------------------- */



    /* ---------------------------------------  学生分布图函数  --------------------------------------- */
    function initMapChart(provinceData) {
        const myEchart = echarts.init(document.querySelector('#map'))
        const dataList = [
            { name: '南海诸岛', value: 0 },
            { name: '北京', value: 0 },
            { name: '天津', value: 0 },
            { name: '上海', value: 0 },
            { name: '重庆', value: 0 },
            { name: '河北', value: 0 },
            { name: '河南', value: 0 },
            { name: '云南', value: 0 },
            { name: '辽宁', value: 0 },
            { name: '黑龙江', value: 0 },
            { name: '湖南', value: 0 },
            { name: '安徽', value: 0 },
            { name: '山东', value: 0 },
            { name: '新疆', value: 0 },
            { name: '江苏', value: 0 },
            { name: '浙江', value: 0 },
            { name: '江西', value: 0 },
            { name: '湖北', value: 0 },
            { name: '广西', value: 0 },
            { name: '甘肃', value: 0 },
            { name: '山西', value: 0 },
            { name: '内蒙古', value: 0 },
            { name: '陕西', value: 0 },
            { name: '吉林', value: 0 },
            { name: '福建', value: 0 },
            { name: '贵州', value: 0 },
            { name: '广东', value: 0 },
            { name: '青海', value: 0 },
            { name: '西藏', value: 0 },
            { name: '四川', value: 0 },
            { name: '宁夏', value: 0 },
            { name: '海南', value: 0 },
            { name: '台湾', value: 0 },
            { name: '香港', value: 0 },
            { name: '澳门', value: 0 },
        ]
        provinceData.forEach(v => {
            dataList.forEach(item => {
                console.log(v.name.indexOf(item.name));
                if (v.name.indexOf(item.name) != -1) { //indexOf方法可以返回某个特定的字符串在字符串汇总首次出现的位置,如果没有找到匹配的字符串则返回-1
                    item.value = v.value
                }
            })
        })
        let option = {
            title: {
                text: '籍贯分布',
                top: 10,
                left: 10,
                textStyle: {
                    fontSize: 16,
                },
            },
            tooltip: {
                trigger: 'item',
                formatter: '{b}: {c} 位学员',
                borderColor: 'transparent',
                backgroundColor: 'rgba(0,0,0,0.5)',
                textStyle: {
                    color: '#fff',
                },
            },
            visualMap: {
                min: 0,
                max: 6,
                left: 'left',
                bottom: '20',
                text: ['6', '0'],
                inRange: {
                    color: ['#ffffff', '#0075F0'],
                },
                show: true,
                left: 40,
            },
            geo: {
                map: 'china',
                roam: false,
                zoom: 1.0,
                label: {
                    normal: {
                        show: true,
                        fontSize: '10',
                        color: 'rgba(0,0,0,0.7)',
                    },
                },
                itemStyle: {
                    normal: {
                        borderColor: 'rgba(0, 0, 0, 0.2)',
                        color: '#e0ffff',
                    },
                    emphasis: {
                        areaColor: '#34D39A',
                        shadowOffsetX: 0,
                        shadowOffsetY: 0,
                        shadowBlur: 20,
                        borderWidth: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)',
                    },
                },
            },
            series: [
                {
                    name: '籍贯分布',
                    type: 'map',
                    geoIndex: 0,
                    data: dataList,
                },
            ],
        }
        myEchart.setOption(option)
    }
    initMapChart(provinceData)
    /* ---------------------------------------  学生分布图函数  --------------------------------------- */

}


